package com.kcwx.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kcwx.common.annotation.Log;
import com.kcwx.common.constant.UserConstants;
import com.kcwx.common.core.controller.BaseController;
import com.kcwx.common.core.domain.AjaxResult;
import com.kcwx.common.core.domain.entity.SysDictType;
import com.kcwx.common.core.page.TableDataInfo;
import com.kcwx.common.enums.BusinessType;
import com.kcwx.common.utils.poi.ExcelUtil;
import com.kcwx.system.service.ISysDictTypeService;

import com.kcwx.shareddomain.common.dto.DeleteDto;
import com.kcwx.shareddomain.common.dto.DelParaValid;
import com.kcwx.shareddomain.system.query.SysDictTypeQuery;
import com.kcwx.shareddomain.system.dto.SysDictTypeEditDto;
import com.kcwx.system.dtomap.SysDictTypeMap;

/**
 * 数据字典信息
 * 
 */
@Api(tags = "系统管理-字典类型管理")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private SysDictTypeMap sysDictTypeMap;

    /**
     * 新增字典类型
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "新增字典类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictTypeEditDto dto)
    {
        SysDictType dict=sysDictTypeMap.AddDtoToEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(getUsername());
        int rowsAffect=dictTypeService.insertDictType(dict);
        return  rowsAffect>0?success():error("新增失败");
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "修改字典类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictTypeEditDto dto)
    {
        SysDictType dict=sysDictTypeMap.EditDtoToEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(getUsername());
        int rowsAffect=dictTypeService.updateDictType(dict);
        return  rowsAffect>0?success():error("修改失败");
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "删除字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody DeleteDto dto)
    {
        Long id=DelParaValid.validDel(dto);
        int rowsAffect=dictTypeService.deleteDictTypeById(id,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }

    /**
     * 批量删除字典类型
     */
    @ApiOperation("批量删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "批量删除字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody DeleteDto dto)
    {
        Long []ids=DelParaValid.validBatchDel(dto);
        int rowsAffect=dictTypeService.deleteDictTypeByIds(ids,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }
    /**
     * 分页查询
     */
    @ApiOperation("分页查询")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @Log(title = "字典类型分页查询", businessType = BusinessType.OTHER)
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysDictTypeQuery dictType)
    {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return success( getDataTable(list));
    }
    /**
     * 根据id获取详细信息
     */
    @ApiOperation("获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @Log(title = "根据id获取字典类型", businessType = BusinessType.OTHER)
    @GetMapping( "/getById")
    public AjaxResult getInfo(Long id)
    {
        return success(dictTypeService.selectDictTypeById(id));
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation("刷新字典缓存")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "刷新字典缓存", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        dictTypeService.resetDictCache();
        return success();
    }
    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @Log(title = "获取字典选择框列表", businessType = BusinessType.OTHER)
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
